<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解MySQL InnoDB B+树结构</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4, .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #2b5876 0%, #4e4376 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .key-point {
            position: relative;
            padding-left: 1.5rem;
        }
        .key-point:before {
            content: "";
            position: absolute;
            left: 0;
            top: 0.7em;
            height: 8px;
            width: 8px;
            background: #4e4376;
            border-radius: 50%;
        }
        .highlight-box {
            border-left: 4px solid #4e4376;
            background-color: rgba(78, 67, 118, 0.05);
        }
        .mermaid {
            background-color: white;
            border-radius: 0.5rem;
            padding: 2rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-2/3 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">深入理解MySQL InnoDB的B+树结构</h1>
                    <p class="text-xl md:text-2xl opacity-90 mb-8">探索数据库索引背后的核心数据结构与优化原理</p>
                    <div class="flex items-center space-x-4">
                        <div class="flex items-center">
                            <i class="fas fa-database text-xl mr-2"></i>
                            <span>存储引擎</span>
                        </div>
                        <div class="flex items-center">
                            <i class="fas fa-project-diagram text-xl mr-2"></i>
                            <span>数据结构</span>
                        </div>
                        <div class="flex items-center">
                            <i class="fas fa-tachometer-alt text-xl mr-2"></i>
                            <span>性能优化</span>
                        </div>
                    </div>
                </div>
                <div class="md:w-1/3 flex justify-center">
                    <div class="w-64 h-64 bg-white bg-opacity-10 rounded-full flex items-center justify-center">
                        <i class="fas fa-sitemap text-6xl opacity-70"></i>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl py-12 px-4 md:px-0">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-purple-700 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">核心概念</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <p class="text-lg leading-relaxed mb-6">在MySQL的InnoDB存储引擎中，B+树的节点大小通常与数据库引擎的页面大小（Page Size）直接相关。默认情况下，InnoDB的页面大小为16KB（16384字节），这也是B+树每个节点的标准大小。</p>
                
                <div class="highlight-box p-6 mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-purple-800"><i class="fas fa-lightbulb mr-2"></i>关键洞察</h3>
                    <p class="key-point mb-2">B+树节点大小 = 存储引擎页面大小</p>
                    <p class="key-point mb-2">默认16KB是性能与效率的平衡点</p>
                    <p class="key-point">页面大小配置影响I/O和内存使用</p>
                </div>
            </div>
        </section>

        <!-- Key Sections -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-purple-700 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">深入解析</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <!-- Card 1 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                <i class="fas fa-cog text-purple-700"></i>
                            </div>
                            <h3 class="text-xl font-bold text-gray-800">页面大小与节点结构</h3>
                        </div>
                        <p class="text-gray-700 mb-4">在InnoDB中，B+树的每个节点都存储在一个或多个16KB的页面中。叶子节点包含索引列和指向实际数据记录的指针（聚簇索引可能包含实际数据行），而非叶子节点包含索引列和子节点指针。</p>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <p class="text-sm text-gray-600"><i class="fas fa-info-circle mr-2 text-purple-600"></i>节点大小直接影响B+树的高度，进而影响查询性能。</p>
                        </div>
                    </div>
                </div>

                <!-- Card 2 -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                <i class="fas fa-sliders-h text-purple-700"></i>
                            </div>
                            <h3 class="text-xl font-bold text-gray-800">页面大小配置</h3>
                        </div>
                        <p class="text-gray-700 mb-4">虽然默认是16KB，但通过<code class="bg-gray-100 px-2 py-1 rounded">innodb_page_size</code>参数可以调整为4KB、8KB、16KB或64KB。这会影响B+树的节点大小，进而影响磁盘I/O性能和内存缓存效率。</p>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>更小的页面：减少内存占用，增加I/O</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>更大的页面：减少I/O，增加内存需求</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-purple-700 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">B+树结构可视化</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                <div class="mermaid">
                    graph TD
                        A[非叶子节点] -->|指针| B[非叶子节点]
                        A -->|指针| C[非叶子节点]
                        B -->|指针| D[叶子节点]
                        B -->|指针| E[叶子节点]
                        C -->|指针| F[叶子节点]
                        C -->|指针| G[叶子节点]
                        D[存储键值+数据指针]
                        E[存储键值+数据指针]
                        F[存储键值+数据指针]
                        G[存储键值+数据指针]
                </div>
                <div class="mt-6 grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-semibold text-lg mb-3 text-purple-800"><i class="fas fa-layer-group mr-2"></i>非叶子节点</h4>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-xs text-purple-600 mt-1 mr-2"></i>
                                <span>包含索引键值和子节点指针</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-xs text-purple-600 mt-1 mr-2"></i>
                                <span>支持快速导航到目标叶子节点</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-semibold text-lg mb-3 text-purple-800"><i class="fas fa-leaf mr-2"></i>叶子节点</h4>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-xs text-purple-600 mt-1 mr-2"></i>
                                <span>存储实际数据或指向数据的指针</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-xs text-purple-600 mt-1 mr-2"></i>
                                <span>通过链表连接，支持范围查询</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Performance Factors -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-purple-700 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">性能影响因素</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <!-- Factor 1 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-purple-700 text-3xl mb-4">
                        <i class="fas fa-ruler-combined"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">行长度</h3>
                    <p class="text-gray-700">短行可以在一个节点中存储更多记录，减少树的高度。长行或包含BLOB/TEXT数据会减少每节点记录数。</p>
                </div>
                
                <!-- Factor 2 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-purple-700 text-3xl mb-4">
                        <i class="fas fa-sitemap"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">索引类型</h3>
                    <p class="text-gray-700">聚簇索引叶子节点存储完整数据行，占用更多空间。二级索引只存储索引列+主键，占用空间较少。</p>
                </div>
                
                <!-- Factor 3 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="text-purple-700 text-3xl mb-4">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">硬件特性</h3>
                    <p class="text-gray-700">16KB页面大小在现代SSD和HDD上都能表现良好，平衡了I/O次数和内存使用效率。</p>
                </div>
            </div>
        </section>

        <!-- Why 16KB -->
        <section>
            <div class="flex items-center mb-6">
                <div class="w-12 h-1 bg-purple-700 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">为什么选择16KB？</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-purple-800">性能平衡点</h3>
                        <p class="text-gray-700 mb-6">16KB页面大小是现代数据库系统中经过充分验证的折中方案，在多种硬件配置和应用场景下都能提供稳定的性能表现。</p>
                        
                        <div class="flex items-start mb-4">
                            <div class="flex-shrink-0 mt-1">
                                <i class="fas fa-check-circle text-green-500 mr-3"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-800">减少I/O操作</h4>
                                <p class="text-gray-600 text-sm">足够大的页面减少了磁盘I/O次数，特别是对于范围查询</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start mb-4">
                            <div class="flex-shrink-0 mt-1">
                                <i class="fas fa-check-circle text-green-500 mr-3"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-800">高效内存利用</h4>
                                <p class="text-gray-600 text-sm">在现代服务器内存容量下，16KB页面能有效利用缓存</p>
                            </div>
                        </div>
                    </div>
                    
                    <div>
                        <div class="bg-purple-50 rounded-lg p-6 h-full">
                            <h3 class="text-xl font-bold mb-4 text-purple-800">配置建议</h3>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <i class="fas fa-database text-purple-600 mt-1 mr-3"></i>
                                    <div>
                                        <p class="font-medium">OLTP系统</p>
                                        <p class="text-sm text-gray-600">保持默认16KB，平衡点查询和更新性能</p>
                                    </div>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-server text-purple-600 mt-1 mr-3"></i>
                                    <div>
                                        <p class="font-medium">分析型系统</p>
                                        <p class="text-sm text-gray-600">考虑更大页面(32KB/64KB)减少扫描I/O</p>
                                    </div>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-mobile-alt text-purple-600 mt-1 mr-3"></i>
                                    <div>
                                        <p class="font-medium">嵌入式系统</p>
                                        <p class="text-sm text-gray-600">内存受限时可尝试8KB页面</p>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>